#ifndef _LUA_HPP_
#define _LUA_HPP_

extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "lobject.h"
#include "lstate.h"
#include "lundump.h"
}

#include "State.hpp"

#define LUA_COMPILER_STRIPPING 1 // 1 == on 0 == off
#define LUA_COMPILER_TMP "/tmp/lct.out"

class State;

struct chunkStruct
{
	char *target;
	char *fileName;
	size_t size;
	
	chunkStruct(const char *fname)
	{
		size_t len = strlen(fname) + 1;
		fileName = (char*) malloc(sizeof(char)*len);
		strcpy(fileName, fname);
		size = 0;
		target = NULL;
	}
	
	~chunkStruct()
	{
		if(target) free(target);
		if(fileName) free(fileName);
	}
	
};

class LuaWrapper
{
public:
	LuaWrapper(State *s, const std::string &n, const std::string &fileName);
	~LuaWrapper();
	
	// chunk ops
	int runChunk(chunkStruct *chunk);
	
	// get variable ops
	int getGlobalInt( const char *name, int &var );
	int getGlobalString( const char *name, std::string &str );
	int getStringArray( const char *name, int max, std::string **str );
	int getIntArray( const char *name, int max, int **var );
	int getGlobalBool( const char *name, bool &b );
	int getGlobalUShort( const char *name, unsigned short &var );
	
	// set variable ops
	int setGlobalInt( const char *name, signed long int val);
	
	static chunkStruct *loadChunk (const char *fileName, State *st);
	
	// specific lua functions for terminal integration
	static int luaPrintHandler(lua_State *L);
	static int luaIOWriteHandler(lua_State *L);
	
protected:
	lua_State *l;
	
	std::string name;
	State *st;
	
	static int process( lua_State *L );
	static int writer( lua_State *L, const void *p, size_t size, void *u );
	
private:
	
};

#endif // _LUA_HPP_
